<template>
  <div class="p-2 modal_main">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" size="small" label-width="80">
            <el-form-item label="算法名称" prop="algorithmName">
              <el-input v-model="queryParams.algorithmName" placeholder="请输入算法名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="算法分类" prop="loadName">
              <el-select v-model="queryParams.loadType" clearable>
                <el-option v-for="(item, index) in pageLoadTypes" :key="'type' + index" :label="item.label" :value="item.label" />
              </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="loadType">
              <el-select v-model="queryParams.loadType" clearable>
                <el-option v-for="(item, index) in pageStates" :key="'name' + index" :label="item.label" :value="item.label" />
              </el-select>
            </el-form-item>
            <el-form-item label="创建日期">
              <el-date-picker
                v-model="dateRangeCreateTime"
                style="width: 240px"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
              ></el-date-picker>
            </el-form-item>
            <el-form-item style="padding-left: 80px">
              <el-button size="small" type="primary" @click="handleQuery">查询</el-button>
              <el-button size="small" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-if="showTable" v-loading="loading" :data="tableDatas">
        <el-table-column label="序号" width="50" type="index" align="center">
          <template #default="scope">
            <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="算法名称" align="center" prop="algorithmName" width="280" show-overflow-tooltip></el-table-column>
        <el-table-column label="算法分类" align="center" prop="loadType"></el-table-column>
        <el-table-column label="版本号" align="center" prop="version" />
        <el-table-column label="状态" align="center" prop="state" width="200">
          <!-- <template #default="scope">
              {{ getLabelByValue(pageStates, scope.row.state) }}
            </template> -->
        </el-table-column>
        <el-table-column label="更新人" align="center" prop="createBy" />
        <el-table-column label="更新日期" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="stage"></el-table-column>
        <el-table-column label="操作" align="center" width="210">
          <template #default="scope">
            <el-button v-hasPermi="['algorithm:management:detail']" size="small" link type="primary" @click="handleDetail(scope.row)">详情</el-button>
            <el-button
              v-hasPermi="['algorithm:management:edit']"
              size="small"
              link
              type="primary"
              :disabled="scope.row.state === '已作废'"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagenation">
        <pagination
          v-show="total > 0"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          layout="prev, pager, next,jumper"
          :total="total"
          @pagination="getList"
        />
      </div>
    </el-card>
    <el-dialog v-if="detailDialogShow" v-model="detailDialogShow" :title="title" width="800px" :before-close="handleDialogClose">
      <AlgorithmDetailForm ref="algorithmDetailFormRef" :type="type" :data-id="dataId" />
      <template #footer>
        <div class="dialog-footer">
          <el-button v-if="type === 'edit'" type="primary" @click="handleSureClick">确定</el-button>
          <el-button @click="handleDialogClose">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="loadAlgorithmManagement" lang="ts">
import {} from '@/api/loadProcessingAlgorithm/index';
import { loadNames, algorithmStates } from '@/api/loadProcessingAlgorithm/index';
import { getAlgorithmList } from '@/api/loadProcessingAlgorithm/loadAlgorithmManagement/index';
import { dataForm, dataQuery, dataVO } from '@/api/loadProcessingAlgorithm/loadAlgorithmManagement/type';
import AlgorithmDetailForm from './algorithmDetailForm.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const tableDatas = ref<dataVO[]>([{ id: '' }]);
const showTable = ref(true);
const loading = ref(false);
const showSearch = ref(true);
const total = ref(0);
const dateRangeCreateTime = ref<[DateModelType, DateModelType]>(['', '']);
const queryFormRef = ref<ElFormInstance>();
const detailDialogShow = ref(false);
const dataId = ref('');
const title = ref('');
const type = ref('view');
const pageLoadNames = ref(loadNames);
const pageLoadTypes = ref([]);
const pageStates = ref(algorithmStates);
const queryParams = reactive<dataQuery>({
  pageNum: 1,
  pageSize: 10,
  algorithmName: '',
  loadType: '',
  state: ''
});
/** 查询模型列表 */
const getList = async () => {
  loading.value = true;
  const response = await getAlgorithmList(proxy?.addDateRange(queryParams, dateRangeCreateTime.value, 'CreateTime'));
  tableDatas.value = response.rows;
  total.value = response.total;
  loading.value = false;
  showTable.value = true;
};
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  showTable.value = false;
  dateRangeCreateTime.value = ['', ''];
  queryFormRef.value?.resetFields();
  handleQuery();
}
const handleDetail = (row) => {
  dataId.value = row.id;
  type.value = 'view';
  title.value = '详情';
  detailDialogShow.value = true;
};
const handleEdit = (row) => {
  dataId.value = row.id;
  type.value = 'edit';
  title.value = '编辑';
  detailDialogShow.value = true;
};
const handleSureClick = () => {};
const handleDialogClose = () => {
  detailDialogShow.value = false;
};
const getLoadTypes = () => {
  pageLoadNames.value.forEach((item) => {
    pageLoadTypes.value = [...pageLoadTypes.value, ...item.children];
  });
};
onMounted(() => {
  getList();
  getLoadTypes();
});
</script>
<style lang="scss" scoped>
.modal_main {
  height: calc(100vh - 84px);
  overflow: auto;
}
.pagenation {
  display: flex;
  justify-content: end;
  margin-top: 10px;
}
.dialog-footer {
  text-align: center;
}
:deep(.el-upload) {
  display: block;
}
</style>
